﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using MVCms.Core.Entities;

namespace MVCms.Core.Services
{
    public static class UserService
    {
        /// <summary>
        /// Gets all allowed pages for the specified user
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static IList<VirtualPage> AllowedPages(this MembershipUser user)
        {
            var pages = new List<VirtualPage>();

            foreach (PageRole pageRole in DataAccess.McContext.Load<PageRole>())
            {
                if(pageRole.Users.Any(u=>u.UserName == user.UserName))
                {
                    pages.Add(pageRole.VirtualPage);
                }
            }

            return pages;
        }

        /// <summary>
        /// Gets all allowed PageModules for the specified user
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static IList<PageModule> AllowedPageModules(this MembershipUser user)
        {
            var modules = new List<PageModule>();

            foreach (ModuleRole moduleRole in DataAccess.McContext.Load<ModuleRole>())
            {
                if (moduleRole.Users.Any(u => u.UserName == user.UserName))
                {
                    modules.Add(moduleRole.PageModule);
                }
            }

            return modules;
        }
    }
}
